

#include "atomic_op_namespace.h"

#include <sys/atomic.h>

#ifdef __HAVE_ATOMIC64_OPS

uint64_t fetch_and_and_8(volatile uint64_t *, uint64_t, ...)
    asm("__sync_fetch_and_and_8");

uint64_t
fetch_and_and_8(volatile uint64_t *addr, uint64_t val, ...)
{
	uint64_t old, new;

	do {
		old = *addr;
		new = old & val;
	} while (atomic_cas_64(addr, old, new) != old);
	return old;
}

void
atomic_and_64(volatile uint64_t *addr, uint64_t val)
{
	(void) fetch_and_and_8(addr, val);
}

__strong_alias(__atomic_fetch_and_8,__sync_fetch_and_and_8)

#undef atomic_and_64
atomic_op_alias(atomic_and_64,_atomic_and_64)

#if defined(_LP64)
#undef atomic_and_ulong
atomic_op_alias(atomic_and_ulong,_atomic_and_64)
__strong_alias(_atomic_and_ulong,_atomic_and_64)
#endif /* _LP64 */

#endif
